package com.woniu.zsd.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.zsd.common.web.PageInfo;
import com.woniu.zsd.shopping.model.ProductType;
import com.woniu.zsd.shopping.dao.ProductTypeMapper;
import com.woniu.zsd.shopping.param.QueryProductTypeParam;
import com.woniu.zsd.shopping.service.ProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {
    @Resource
    private ProductTypeMapper productTypeMapper;

    @Override
    public void addProductType(String productTypeName) throws Exception {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("type_name", productTypeName);
        ProductType productType = productTypeMapper.selectOne(wrapper);
        if (productType!= null) {
            throw new Exception("商品类型已存在");
        }
        ProductType productType1 = new ProductType();
        productType1.setTypeName(productTypeName);
        productTypeMapper.insert(productType1);
    }

    @Override
    public void deleteProductType(Integer productTypeId) throws Exception {
        ProductType productType = productTypeMapper.selectById(productTypeId);
        if (productType == null) {
            throw new Exception("商品类型不存在");
        }
        productTypeMapper.deleteById(productTypeId);
    }

    @Override
    public PageInfo queryProductType(QueryProductTypeParam param) throws Exception {
        Page<ProductType> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        if(!StringUtil.isEmpty(param.getTypeName())){
            wrapper.like("type_name", param.getTypeName());
        }
        Page<ProductType> page1 = productTypeMapper.selectPage(page, wrapper);
        PageInfo pageInfo = new PageInfo(page1);
        return pageInfo;
    }
}
